package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.City;
import com.ruoyi.common.core.domain.entity.Institution;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.mapper.SysProvincesMapper;
import com.ruoyi.system.service.IInstitutionService;
import com.ruoyi.system.service.ISysProvincesService;
import com.ruoyi.system.service.impl.SysProvincesServiceImpl;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/itsystem/institution")
public class SysInstitutionController extends BaseController {

    private String prefix = "itsystem/institution";

    @Autowired
    private IInstitutionService institutionService;

    @Autowired
    private ISysProvincesService provincesMapper;

    @GetMapping()
    @RequiresPermissions("itsystem:institution:view")
    public String institution(ModelMap modelMap){
        modelMap.addAttribute("citys", provincesMapper.selectAllCity());
        return prefix + "/institution";
    }

    @PostMapping("/list")
    @RequiresPermissions("itsystem:institution:list")
    @ResponseBody
    public TableDataInfo list(Institution institution)
    {
        java.util.List<Institution> list = institutionService.selectAllInstitution(institution);
        return getDataTable(list);
    }

    //添加机构
    @GetMapping("/add")
    @RequiresPermissions("itsystem:institution:add")
    public String add(ModelMap modelMap){
        List<City> citys = provincesMapper.selectAllCity();
        modelMap.addAttribute("citys", citys);
        return prefix + "/add";
    }

    @PostMapping("/add")
    @RequiresPermissions("itsystem:institution:add")
    @Log(title = "机构管理", businessType = BusinessType.INSERT)
    @ResponseBody
    public AjaxResult addSave(@Validated Institution institution)
    {
        institution.setuId(ShiroUtils.getUserId().intValue());
        if(institutionService.checkPhoneUnique(institution))
        {
            return toAjax(institutionService.addInstitution(institution));
        }
        else
            return error("新增机构'"+institution.getItName()+"'失败，联系方式已存在");
    }

    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public boolean checkPhoneUnique(String itPhone)
    {
        Institution check = new Institution();
        check.setItPhone(itPhone);
        return institutionService.checkPhoneUnique(check);
    }

    //修改机构
    @GetMapping("/edit/{itId}")
    @RequiresPermissions("itsystem:institution:edit")
    public String edit(@PathVariable("itId")Long itId, ModelMap modelMap)
    {
        modelMap.addAttribute("citys", provincesMapper.selectAllCity());
        modelMap.put("institution",institutionService.selectInstitutionById(itId));
        return prefix + "/edit";
    }

    @PostMapping("/edit")
    @RequiresPermissions("itsystem:institution:edit")
    @Log(title = "机构管理", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult editUpdate(@Validated Institution institution)
    {
        if(institutionService.checkPhoneUnique(institution))
        {
            return toAjax(institutionService.updateInstitution(institution));
        }
        else
            return error("更新机构'"+institution.getItName()+"'失败，联系方式已存在");
    }


    //删除机构
    @Log(title = "机构管理",businessType = BusinessType.DELETE)
    @RequiresPermissions("itsystem:institution:remove")
    @PostMapping("/remove/{itId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("itId")Long itId)
    {
        return toAjax(institutionService.removeInstitution(itId));
    }




    //图片上传
    @RequiresPermissions("itsystem:institution:edit")
    @PostMapping("/imgupload")
    @ResponseBody
    public Map<String, Object> imgupload(@RequestParam("img")MultipartFile file) throws IOException {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("errno", Integer.valueOf(1));

        String url = FileUploadUtils.upload(file);
        result.put("errno", Integer.valueOf(0));
        List<String> data = new ArrayList<String>();
        data.add(url);
        result.put("data", data);


        return result;
    }

}
